package BtoB;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import BtoB.util.BtoBConstants;
import BtoB.util.ItemValueObject;
import BtoB.util.ProcessXMLHelper;
import BtoB.util.WebServiceHelper;
import BtoB.util.WholeSaler;


public class PlaceOrders {
	private static Map<String,ItemValueObject> itemSummaryMap;	
	public static void main(String[] args) throws Exception	{
		
		itemSummaryMap = ProcessXMLHelper.getOrderSummary();
		for(String itemNumber: itemSummaryMap.keySet()){
			placeOrder(itemSummaryMap.get(itemNumber));
		}
		
		createXMLOrder(itemSummaryMap);
		System.out.println("Finished placing all the order! You can check it from website! \nFor examle: http://localhost:8080/Proj4413/poFiles/order20111206.xml");
	}
	
	 
	private static void placeOrder(ItemValueObject item){
		final List<WholeSaler> wholeSalers = new ArrayList<WholeSaler>();
		final Comparator<WholeSaler> lowestFirst = new Comparator<WholeSaler>() {
			public int compare(WholeSaler w1, WholeSaler w2) {
			return w1.compareTo(w2);
			}
		};		
		
		try {
			double fromToronto = WebServiceHelper.quote(BtoBConstants.TorontoWholeSalerUrl, item.getItemNumber());		
			double fromVancouver = WebServiceHelper.quote(BtoBConstants.VancouverWholeSalerUrl, item.getItemNumber());
			double fromHalifax = WebServiceHelper.quote(BtoBConstants.HalifaxWholeSalerUrl, item.getItemNumber());
			if(fromToronto > 0){
				wholeSalers.add(new WholeSaler(BtoBConstants.TORONTOWHOLESALER, BtoBConstants.TorontoWholeSalerUrl,fromToronto));
			}
			
			if(fromVancouver > 0){
				wholeSalers.add(new WholeSaler(BtoBConstants.VANCOUVERWHOLESALER, BtoBConstants.VancouverWholeSalerUrl,fromVancouver));
			}
			
			if(fromHalifax > 0){
				wholeSalers.add(new WholeSaler(BtoBConstants.HALIFAXWHOLESALER, BtoBConstants.HalifaxWholeSalerUrl,fromHalifax));
			}			
		
		} catch (Exception e) {
			System.out.println("Quoting error occurs!");
		}
		Collections.sort(wholeSalers, lowestFirst);
		
		if(wholeSalers.size()>0){
			WholeSaler ws = wholeSalers.get(0);
			try {
				WebServiceHelper.order(ws.getUrl(), item.getItemNumber(), item.getOrderQty(), BtoBConstants.KEY);
			} catch (Exception e) {
				System.out.println("Placing order error! Please check the connection.");
			}
			updateOrderSummary(item.getItemNumber(), wholeSalers.get(0));
		}
	}
	
	private static void updateOrderSummary(String itemNumber, WholeSaler wholeSaler){
		ItemValueObject itemVO = new ItemValueObject();
		ItemValueObject item = itemSummaryMap.get(itemNumber);
		itemVO.setBidingPrice(wholeSaler.getPrice());
		itemVO.setWholeSaler(wholeSaler.getName());
		itemVO.setItemName(item.getItemName());
		itemVO.setItemNumber(item.getItemNumber());
		itemVO.setOrderQty(item.getOrderQty());
		itemSummaryMap.put(itemNumber, itemVO);
	}
	
	private static void createXMLOrder(Map<String, ItemValueObject> items) {		
		try	{
			String orderDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			String strDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
			String filePath = BtoBConstants.ORDERSUMMARYRELIVATEPATH ;
			String filename = "order" + strDate + ".xml";
			File file = new File(filePath + filename);
			PrintStream ps = new PrintStream(new FileOutputStream(file));
			ps.println("<?xml version=\"1.0\"?>");
			ps.println("<?xml-stylesheet type=\"text/xsl\" href=\"orderSummary.xsl\"?>");			
			ps.println("<order orderDate=\"" + orderDate + "\"" + ">");				
				ps.println("<items>");			
					for (String itemNumber : items.keySet()){
						ItemValueObject itemVO = items.get(itemNumber);
						ps.println("<item number=\"" + itemVO.getItemNumber() +"\">");
							ps.println("<name>" + itemVO.getItemName() + "</name>");
							ps.println("<price>" + itemVO.getBidingPrice() + "</price>");
							ps.println("<quantity>" + itemVO.getOrderQty() + "</quantity>");
							ps.println("<wholeSaler>" + itemVO.getWholeSaler() + "</wholeSaler>");							
						ps.println("</item>");					
					}
				ps.println("</items>");
			ps.println("</order>");
		} 
		catch (Exception e){
			System.out.println("file writing error!\n" + e);
		}		
	}
	
}

